#include<iostream>
using namespace std;
const int N = 1e3 + 10;
int a[N], b[N];
int f[N][N];
int n;
int main()
{
	cin >> n;
	for (int i = 1; i <= n; i++)cin >> a[i];
	for (int i = 1; i <= n; i++)cin >> b[i];
	/*for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= n; j++) {
			f[i][j] = f[i - 1][j];
			if (a[i] == b[j]) {
				int maxv = 1;
				for (int k = 1; k <= j; k++) {
					if (a[i] > b[k]) {
						maxv = max(maxv, f[i - 1][k] + 1);
					}
				}
				f[i][j] = max(f[i][j], maxv);
			}
		}
	}*/
	for (int i = 1; i <= n; i++) {
		int ma = 1;
		for (int j = 1; j <= n; j++) {
			f[i][j] = f[i - 1][j];
			if (a[i] == b[j]) {
				f[i][j] = max(f[i][j], ma);
			}
			if (a[i] > b[j]) {
				ma = max(ma, f[i - 1][j] + 1);
			}
		}
	}
	int ans = 0;
	for (int i = 1; i <= n; i++) {
		ans = max(ans, f[n][i]);
	}
	cout << ans << endl;
	return 0;
}